package main

import (
	"flag"
	"log"
	"os"
)

/**
* Author: dinglin
* Date: 2021/6/23 1:48 下午
 */

type Config struct {
	fromHost     string
	toHost       string
	fromIndex    string
	toIndex      string
	repository   string
	snapshotName string
	operate      string
}
type doWork func()

var (
	config   = Config{}
	confPath string
	workFunc = map[string]doWork{
		"reindex":  doReindex,
		"restore":  doRestore,
		"snapshot": doSnapshot,
	}
)

/*
	./main -operate=reindex -from_host=http://1.2.3.4:9200 -from_index=ab -to_index=cd
	./main -operate=restore -to_host=http://1.2.3.4:9200 -repository=xxx -snapshot_name=xx -to_index=xxx
	./main -operate=snapshot -to_host=http://1.2.3.4:9200 -repository=xxx -snapshot_name=xx -from_index=xxx
	./main -conf=xxx.json
*/

func init() {
	log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
	flag.StringVar(&confPath, "conf", "", "-conf=xxx.json")
	flag.StringVar(&config.fromHost, "from_host", "", "-from=http://abcd.com:9200")
	flag.StringVar(&config.toHost, "to_host", "", "-to_host=http://abcd.com:9200")
	flag.StringVar(&config.fromIndex, "from_index", "", "-from_index=xx")
	flag.StringVar(&config.toIndex, "to_index", "", "-to_index=xxx")
	flag.StringVar(&config.repository, "repository", "", "-repository=xxx")
	flag.StringVar(&config.operate, "operate", "", "-operate=reindex|restore|snapshot")
	flag.StringVar(&config.snapshotName, "snapshot_name", "", "-snapshot_name=xx")
}

func main() {
	flag.Parse()
	if confPath != "" {
		parseConfig(confPath)
	}
	if fn, ok := workFunc[config.operate]; ok {
		fn()
		return
	}

	log.Printf("invalid operate(%s)", config.operate)
	flag.PrintDefaults()
	os.Exit(-1)
}
